package com.yx.frame.builder.xml;

public class MethodMencacheXml {

	String cached = "false";

	String staticfile = "false";

	String catcheTimeout = "0";

	String staticfileDays = "0";

	String[] keys;

	String isPrivate = "false";

	String isPagable = "false";

	public MethodMencacheXml(String cached, String staticfile,
			String catcheTimeout, String staticfileDays, String keyString,
			String isPrivate, String isPagable) {
		if ("".equals(keyString.trim())) {
			keys = new String[0];
		} else {
			this.keys = keyString.split(",");
		}
		this.cached = cached;
		this.staticfile = staticfile;
		this.catcheTimeout = catcheTimeout;
		this.staticfileDays = staticfileDays;
		this.isPrivate = isPrivate;
		this.isPagable = isPagable;

	}

	public MethodMencacheXml(String cached, String staticfile,
			String catcheTimeout, String staticfileDays, String[] keys,
			String isPrivate, String isPagable) {
		this.cached = cached;
		this.staticfile = staticfile;
		this.catcheTimeout = catcheTimeout;
		this.staticfileDays = staticfileDays;
		this.keys = keys;
		this.isPrivate = isPrivate;
		this.isPagable = isPagable;
	}

	public String getCached() {
		return cached;
	}

	public String getStaticfile() {
		return staticfile;
	}

	public String getStaticfileDays() {
		if(staticfileDays==null||"null".equals(staticfileDays)){
			staticfileDays="0";
		}
		return staticfileDays;
	}

	public String getCatcheTimeout() {
		if(catcheTimeout==null||"null".equals(catcheTimeout)){
			catcheTimeout="0";
		}
		return catcheTimeout;
	}

	public String[] getKeys() {
		return keys;
	}

	public String getIsPrivate() {
		return isPrivate;
	}

	public String getIsPagable() {
		return isPagable;
	}

	public String getKeyString() {
		if (keys == null) {
			return "";
		}
		String keyString = "";
		for (int i = 0; i < keys.length; i++) {
			keyString = keyString + keys[i] + ",";
		}
		if (keys.length > 0) {
			keyString = keyString.substring(0, keyString.length() - 1);
		}
		return keyString;
	}

	public String getMencacheKey(int actionId, String isQueryPagable) {
		String mencacheKey = "\"Action" + actionId + "\"";
		if ("true".equals(getIsPrivate())) {
			mencacheKey = mencacheKey + "+\"_User\"+user.getUserId()";
		}
		for (int i = 0; i < keys.length; i++) {
			mencacheKey = mencacheKey + "+\"_\"+" + keys[i];
		}
		if ("true".equals(isQueryPagable) && "true".equals(getIsPagable())) {
			mencacheKey = mencacheKey + "+\"_\"+pageIndex" + "+\"_\"+pageSize";
		}
		return mencacheKey;
	}

	public StringBuffer toXml() {
		StringBuffer xml = new StringBuffer();
		xml.append("		<Mencache cached=\"" + getCached() + "\"  staticfile=\""
				+ this.getStaticfile() + "\" key=\"" + getKeyString()
				+ "\" catcheTimeout=\"" + catcheTimeout
				+ "\" staticfileDays=\"" + staticfileDays + "\" private=\""
				+ this.getIsPrivate() + "\" page=\"" + getIsPagable()
				+ "\" />\r\n");
		return xml;
	}

}
